﻿Imports PhDir.Repository
Imports PhDirForm.Util
Imports PhDir.Model
Imports System.ComponentModel

Public Class InsertPhone


    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.PhoneRepository = PhDir.Main.Instance.PhoneRepository
        Me.SetStyle(ControlStyles.OptimizedDoubleBuffer Or
                     ControlStyles.UserPaint Or
                     ControlStyles.AllPaintingInWmPaint, True)
    End Sub

    Private Property PhoneRepository As PhoneRepository

    Private Sub ResetButton_Click(sender As System.Object, e As System.EventArgs) Handles ResetButton.Click
        Me.UserIdTextBox.Text = String.Empty
        Me.PhoneMaskedBox.Text = String.Empty
    End Sub

    Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click
        Dim userId = 0
        Int32.TryParse(Me.UserIdTextBox.Text, userId)
        Dim user = New User With {.UserId = userId}
        Dim phone = New Phone With {.User = user, .PhoneNumber = Me.PhoneMaskedBox.Text}
        Dim isSuccess = Me.PhoneRepository.Insert(phone)

        If isSuccess = True Then
            MessageBox.Show("Operation Succeed")
        Else
            MessageBox.Show("Operation Failed")
        End If
    End Sub
End Class
